package com.bml.architect.bio;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class BioScoket {

    public static void main(String[] args) throws IOException {




        ServerSocket serverSocket = new ServerSocket(8000);
        while (true){
            System.out.println("等待客户端连接....");
             Socket socket = serverSocket.accept();
            System.out.println("有客户端连接过来了");
            //handlerConn(socket);
            new Thread(() ->{
                try {
                    handlerConn(socket);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }

    private static void handlerConn(Socket socket) throws IOException {

        System.out.println("开始读取客户端发过来的数据");
        byte [] data  = new byte[1024];
        //如果客户端连接了没有发送数据，那么一直会阻塞在这里
        int read = socket.getInputStream().read(data);
        System.out.println("数据读取完毕");
        if(read != -1){
            System.out.println("收到客户端数据:"+new String(data,0,read));
        }
        ///socket.getOutputStream().write("recive succ".getBytes());
        //socket.getOutputStream().flush();
    }
}